package health.lm.com.mine.fragment;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.rxbus.RxBus;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.fragment.XLazyFragment;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.CashAccountAdapter;
import health.lm.com.mine.entity.CashAccountEntity;
import health.lm.com.mine.present.CashAccountPresent;
import health.lm.com.router.ARouterConstant;
import io.reactivex.functions.Consumer;

import static com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext;

/**
 * 提现账户
 */
public class CashAccountFragment extends XLazyFragment<CashAccountPresent>{
    @BindView(R2.id.rlv_card)
    RecyclerView rlvCard;
    @BindView(R2.id.tv_add_card)
    TextView tvAddCard;
    private CashAccountAdapter partAdapter;
    private List<CashAccountEntity> entities;
    private String id="";
    private final static  String CARD = "1";
    private final static  String ZFB = "2";
    private Paint paint;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_cash_account;
    }

    @Override
    public CashAccountPresent newP() {
        return new CashAccountPresent();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
        initPartAdapter();
        Bundle bundle = getArguments();
        if(bundle!=null){
            id = bundle.getString("id");
            getP().getData(id);
            if(StringUtils.equals(CARD,id)){
                tvAddCard.setText("添加新的银行卡");
            }else{
                tvAddCard.setText("添加新的支付宝");
            }
        }
        RxView.clicks(tvAddCard)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if(StringUtils.equals(CARD,id)){
                            gotoActivity(ARouterConstant.EditBankCardActivity);
                        }else{
                            gotoActivity(ARouterConstant.EditAlipayAccountActivity);
                        }
                    }
                });
    }
    public static CashAccountFragment getInstance(String id) {
        CashAccountFragment fragment = new CashAccountFragment();
        Bundle args = new Bundle();
        args.putSerializable("id", id);
        fragment.setArguments(args);
        return fragment;
    }
    /**
     * 初始化银行卡/支付宝账户
     */
    private void initPartAdapter() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(40);
        paint.setColor(Color.BLACK);
        entities = new ArrayList<>();
        partAdapter = new CashAccountAdapter(entities);
        rlvCard.setLayoutManager(new LinearLayoutManager(mContext));
        rlvCard.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)
                .setDividerColor(0xFFeeeeee)
                .setOrientation(RecyclerView.VERTICAL)
                .setIsShowLastDivide(true)));

        ItemDragAndSwipeCallback mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(partAdapter);
        ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
        mItemTouchHelper.attachToRecyclerView(rlvCard);
        partAdapter.enableSwipeItem();
        mItemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START );
        partAdapter.setOnItemSwipeListener(onItemSwipeListener);
        rlvCard.setAdapter(partAdapter);
        partAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CashAccountEntity entity = entities.get(position);
                RxBus.getDefault().post(entity,"card");
            }
        });
    }
    String TAG = "2222";
    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "view swiped start: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            holder.setTextColor(R.id.tv_bank_card_name,ContextCompat.getColor(context, R.color.color_3E404D));
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View reset: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            holder.setTextColor(R.id.tv_bank_card_name, ContextCompat.getColor(context, R.color.color_ff6d09));
        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View Swiped: " + pos);
            ToastUtils.showShort("View Swiped: " + pos);
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
            canvas.drawColor(ContextCompat.getColor(context, R.color.bg_divider));
            canvas.drawText("左滑删除账户", 20, 90, paint);
        }
    };


    public void getDataSuccess(List<CashAccountEntity> entities){
        this.entities = entities;
        notifyPartData();
    }

    /**
     * 提现账户
     */
    private void notifyPartData() {
        if (entities == null) {
            return;
        }
        partAdapter.setNewData(entities);
    }



}
